package com.sky.mapper;

import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ReportMapper {

    /**
     * 根据时间查询当日营业额
     *
     * @param begin
     * @param end
     * @param status
     * @return
     */
    @Select("select sum(amount) from orders where delivery_time > #{begin} and delivery_time < #{end} and status = #{status}")
    BigDecimal selectByDateNow(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end, @Param("status") Integer status);

    /**
     * 查询当日用户量
     *
     * @param begin
     * @param end
     * @param status
     * @return
     */
    @Select("select count(user_id) from orders where delivery_time > #{begin} and delivery_time < #{end} and status = #{status}")
    Integer selectUserCountByDate(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end, @Param("status") Integer status);

    /**
     * 查询用户总量
     * @return
     */
    @Select("select count(user_id) from orders where status = #{status}")
    Integer selectTotalUser(@Param("status") Integer status);

    /**
     * 查询当日订单数
     * @param begin
     * @param end
     */
    @Select("select count(*) from orders where delivery_time > #{begin} and delivery_time < #{end}")
    Integer selectOrderCount(@Param("begin")LocalDateTime begin,  @Param("end")LocalDateTime end);

    /**
     * 查询当日已完成的订单数
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from orders where delivery_time > #{begin} and delivery_time < #{end} and status = #{status}")
    Integer selectComplateOrder(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end, @Param("status") Integer status);

    /**
     * 查询总订单数
     * @return
     */
    @Select("select count(*) from orders")
    Integer selectAllOrdersAmount();

    /**
     * 查询总共有效订单数量
     * @param status
     * @return
     */
    @Select("select count(*) from orders where status = #{status}")
    Integer selectTrueOrderAmount(@Param("status") Integer status);

    /**
     * 查询日期范围的订单id
     * @param localDateBegin
     * @param localDateEnd
     * @param status
     * @return
     */
    @Select("select id from orders where delivery_time > #{begin} and delivery_time < #{end} and status = #{status} ")
    List<Integer> selectOrderCommodityId(@Param("begin") LocalDateTime localDateBegin,@Param("end") LocalDateTime localDateEnd,@Param("status") Integer status);

    /**
     * 根据订单id查询当日销量前10
     */
    @Select("select distinct name  from order_detail where order_id = #{id} group by name order by count(name) desc limit 10")
    List<String> selectTopCommodifyName(@Param("id") Integer id);

    /**
     * 查询前10销售量
     * @param name
     * @param orderId
     * @return
     */
    @Select("select number from order_detail where name = #{name} and order_id = #{orderId}")
    List<Integer> selectTopNumberByName(@Param("name") String name, @Param("orderId") Integer orderId);

    /**
     * 增加方法
     * @param name
     * @param orderId
     * @param step
     * @return
     */
    @Select("select number + #{step} from order_detail where name = #{name} and order_id = #{orderId}")
    List<Integer> selectTopNumberByNameStep(@Param("name") String name, @Param("orderId") Integer orderId, @Param("step") Integer step);

    /**
     * 查询当日新增用户
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(id) from user where create_time > #{begin} and create_time < #{end}")
    Integer selectUserNow(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);
}
